import TUIO.TuioProcessing;
import processing.core.*;
import ddf.minim.*;
public class AddListener extends PApplet {
    /**
     * Add Listener
     * by Damien Di Fede.
     *  
     * This sketch demonstrates how to use the <code>addListener</code> method of a <code>Recordable</code> class. 
     * The class used here is <code>AudioPlayer</code>, but you can also add listeners to <code>AudioInput</code>, 
     * <code>AudioOutput</code>, and <code>AudioSample</code> objects. The class defined in waveform.pde implements 
     * the <code>AudioListener</code> interface and can therefore be added as a listener to <code>groove</code>.
     */
    TuioProcessing tuioClient;

    Minim minim;
    AudioPlayer groove;
    WaveformRenderer waveform;
    int rectX, rectY;   
    int rectSize = 50;
    
    boolean rectOver = false;
    private int currentColor;
    
    public void setup()
    {
        rectX = width-rectSize-10;
        rectY = height/2-rectSize/2;
        size(512, 200/*, P2D*/);
        currentColor=0;
      minim = new Minim(this);
      groove = minim.loadFile("307-push_-_universal_nation.mp3", 512);
      groove.loop();
      waveform = new WaveformRenderer(this);
      groove.addListener(waveform);
      tuioClient  = new TuioProcessing(this);
    }

    public void draw()
    {
      background(currentColor);
      // see waveform.pde for an explanation of how this works
      waveform.draw();
      stroke(200,200,100);
      rect(rectX, rectY, rectSize, rectSize);
      
    }
    public void mousePressed(){
        if(mouseX<rectX+rectSize&&mouseX>rectX&&mouseY>rectY&&mouseY<rectY+rectSize){
            int pink = color (255,00,00);
            currentColor=pink;
            groove.close();
            groove = minim.loadFile("Lasgo - Lost.mp3");
            groove.loop();
            waveform = new WaveformRenderer(this);
            groove.addListener(waveform);
        }else{
            currentColor=0;
        }
    }
   public void stop()
    {
      // always close Minim audio classes when you are done with them
      groove.close();
      // always stop Minim before exiting.
      minim.stop();
      super.stop();
    }

}
